package clases;

import javax.swing.table.DefaultTableModel;
import java.sql.Statement;
import java.sql.ResultSetMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * @web http://www.hcomputacion.com
 * @author Pablo J. Heurtley
 * @version pre-alpha 0.01
 */
public class Tabla
{
    Statement stm;
    ResultSet rs;
    ResultSetMetaData metaData;
    int columnas = 0;
    public DefaultTableModel ActualizarTabla() throws SQLException
    {
        ConexionMySQL conexion = new ConexionMySQL();
        DefaultTableModel modelo = new DefaultTableModel();
        try 
        {
            stm = conexion.conexion.createStatement();
        } 
        catch (SQLException ex) 
        {
            
        }
        
        try 
        {
            rs = stm.executeQuery("SELECT id, nombre, apellido, numcheque, cuit, monto, femision, fvencimiento, fmovimiento FROM "+Config.Tabla+" ORDER BY "+Config.ordenar);
        } 
        catch (SQLException ex) 
        {

        }
        try 
        {
            metaData = rs.getMetaData();
        } 
        catch (SQLException ex) 
        {
        
        }
        try 
        {
            columnas = metaData.getColumnCount();
        } 
        catch (SQLException ex) 
        {
            
        }
        for (int i = 1; i <= columnas; i++)
        {
            modelo.addColumn(metaData.getColumnLabel(i));
        }
        
        while (rs.next())
        {
            Object[] fila = new Object[columnas];
            for (int i = 1; i <= columnas; i++)
            {
                fila[i-1]=rs.getObject(i);
            }
            modelo.addRow(fila);
        }
        rs.close();
        stm.close();
        return modelo;
    }
    
    public DefaultTableModel Busqueda(String tipo, String valor) throws SQLException
    {
        ConexionMySQL conexion = new ConexionMySQL();
        DefaultTableModel modelo = new DefaultTableModel();
        try 
        {
            stm = conexion.conexion.createStatement();
        } 
        catch (SQLException ex) 
        {
            
        }
        
        try 
        {
            String querry ="SELECT id, nombre, apellido, numcheque, cuit, monto, femision, fvencimiento, fmovimiento FROM "+Config.Tabla+" WHERE "+tipo+" ='"+valor+"'";
            System.out.println(querry);
            rs = stm.executeQuery("SELECT id, nombre, apellido, numcheque, cuit, monto, femision, fvencimiento, fmovimiento FROM "+Config.Tabla+" WHERE "+tipo+" ='"+valor+"'");
        } 
        catch (SQLException ex) 
        {

        }
        try 
        {
            metaData = rs.getMetaData();
        } 
        catch (SQLException ex) 
        {
        
        }
        try 
        {
            columnas = metaData.getColumnCount();
        } 
        catch (SQLException ex) 
        {
            
        }
        for (int i = 1; i <= columnas; i++)
        {
            modelo.addColumn(metaData.getColumnLabel(i));
        }
        
        while (rs.next())
        {
            Object[] fila = new Object[columnas];
            for (int i = 1; i <= columnas; i++)
            {
                fila[i-1]=rs.getObject(i);
            }
            modelo.addRow(fila);
        }
        rs.close();
        stm.close();
        return modelo;
    }
}
